//
//  BaseNavController.swift
//  MT
//
//  Created by zhang qiang on 2018/11/21.
//  Copyright © 2018年 张强. All rights reserved.
//

import UIKit
import YPNavigationBarTransition
import MTCategory

//open class BaseNavController: UINavigationController, UINavigationControllerDelegate, UINavigationBarDelegate, UIGestureRecognizerDelegate {

open class BaseNavController: YPNavigationController {

    override open func viewDidLoad() {
        super.viewDidLoad()
        //self.mt_enableFullScreenPop(isEnabled: true)
        // Do any additional setup after loading the view.
        self.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.init(hex: 0x333333), NSAttributedString.Key.font:PingFangSCRegularFont(17)]
    }
    
    //重写push方法
    override open func pushViewController(_ viewController: UIViewController, animated: Bool) {
        let count = children.count
        if count > 0 {
            //let value = CGFloat(237 / 255.0)
            //隐藏底部的tabbar
            viewController.hidesBottomBarWhenPushed = true
            //viewController.view.backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1)
            if let vc = viewController as? BaseViewController {
                //统一操作
                vc.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "nav_back"), style: .done, target: self, action: #selector(back))
            }
        }
        super.pushViewController(viewController, animated: animated)
    }

    //推出栈顶控制器
    @objc private func back() {
        popViewController(animated: true)
    }

    override open func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    open override var preferredStatusBarStyle: UIStatusBarStyle {
        return self.topViewController?.preferredStatusBarStyle ?? .default
    }
    
    // Helper function inserted by Swift 4.2 migrator.
    fileprivate func convertFromNSAttributedStringKey(_ input: NSAttributedString.Key) -> String {
        return input.rawValue
    }

    // Helper function inserted by Swift 4.2 migrator.
    fileprivate func convertToOptionalNSAttributedStringKeyDictionary(_ input: [String: Any]?) -> [NSAttributedString.Key: Any]? {
        guard let input = input else { return nil }
        return Dictionary(uniqueKeysWithValues: input.map { key, value in (NSAttributedString.Key(rawValue: key), value)})
    }

}


// MARK: - 扩展方法
extension BaseNavController: NavigationBarConfigureStyle {
    public func yp_navigationBarTintColor() -> UIColor! {
        return nil
    }

    public func yp_navigtionBarConfiguration() -> YPNavigationBarConfigurations {
        return []
    }
    
    public func yp_navigationBackgroundColor() -> UIColor! {
        return .white
    }
}

extension UINavigationController {
    open func selectHome() {
        self.tabBarController?.selectedIndex = 0
    }
    
    open func selectCategory() {
        self.tabBarController?.selectedIndex = 1
    }
    
    open func selectClub() {
        self.tabBarController?.selectedIndex = 2
    }
    open func selectCar() {
        self.tabBarController?.selectedIndex = 3
    }
    
    open func selectProfile() {
        self.tabBarController?.selectedIndex = 4
    }
}
